package cn.com.agameofthrones.AGoTLCGCards;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import cn.com.agameofthrones.AGoTLCGCards.AGoTCardsDB;

public class AGoTContentProvider extends ContentProvider {
	private SQLiteDatabase agotDB;
	
	private static final String AUTHORITY = "cn.com.agameofthrones.AGoTLCGCards.AGoTContentProvider";
	public static final int CARDS = 100;
	public static final int CARD_ID = 110;
	public static final int SETS   = 120;
	public static final int TYPES   = 130;
	public static final int CRESTS =140;
	 
	private static final String CARDS_BASE_PATH = "CARDS";
	public static final Uri SEARCH_CARDS_URI = Uri.parse("content://" + AUTHORITY
	        + "/" + CARDS_BASE_PATH);
	public static final Uri SINGLE_CARD_URI = Uri.parse("content://" + AUTHORITY
	        + "/CARD/");
	public static final Uri SEARCH_SETS_LIST = Uri.parse("content://" + AUTHORITY
	        + "/SETS");
	public static final Uri SEARCH_TYPES_LIST = Uri.parse("content://" + AUTHORITY
	        + "/TYPES");
	public static final Uri SEARCH_CRESTS_LIST = Uri.parse("content://" + AUTHORITY
	        + "/CRESTS");
//	public static final String CONTENT_ITEM_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE
//	        + "/agot-card";
//	public static final String CONTENT_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE
//	        + "/agot-card";
//	
	private static final UriMatcher sURIMatcher = new UriMatcher(
	        UriMatcher.NO_MATCH);
	static {
	    sURIMatcher.addURI(AUTHORITY, CARDS_BASE_PATH, CARDS);
	    sURIMatcher.addURI(AUTHORITY, "CARD/#", CARD_ID);
	    sURIMatcher.addURI(AUTHORITY, "SETS", SETS);
	    sURIMatcher.addURI(AUTHORITY, "TYPES", TYPES);
	    sURIMatcher.addURI(AUTHORITY, "CRESTS", CRESTS);
	}
	@Override
	public int delete(Uri arg0, String arg1, String[] arg2) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public String getType(Uri arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Uri insert(Uri arg0, ContentValues arg1) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean onCreate() {
		agotDB=new AGoTCardsDB(getContext()).getWritableDatabase();
		return (agotDB == null) ? false : true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
			String sortOrder) {
		 SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
		    
		 
		    int uriType = sURIMatcher.match(uri);
		    switch (uriType) {
		    case CARD_ID:
		    	queryBuilder.setTables("t_cards");
		        queryBuilder.appendWhere("_id" + " = \""
		                + uri.getLastPathSegment() + "\"" );  	
		        break;
		    case CARDS:
		        // no filter
		    	queryBuilder.setTables("v_main_search"); // this is a saved view in sqlite db.
		    	//queryBuilder.appendWhere("t_cards.types=t_types._id and t_cards.setsID=t_sets.setsID");
		        break;
		    case SETS:
		    	queryBuilder.setTables("t_sets");
		    	break;
		    case TYPES:
		    	queryBuilder.setTables("t_types");
		    	break;
		    case CRESTS:
		    	queryBuilder.setTables("t_crests");
		    	break;
		    default:
		        throw new IllegalArgumentException("Unknown URI");
		    }
		    //boolean open=agotDB.isOpen();
		 
		    Cursor cursor = queryBuilder.query(agotDB,
			          projection, selection, selectionArgs, null, null, sortOrder);
		    
		  //  cursor.setNotificationUri(getContext().getContentResolver(), uri);
		    return cursor;
	}

	@Override
	public int update(Uri arg0, ContentValues arg1, String arg2, String[] arg3) {
		// TODO Auto-generated method stub
		return 0;
	}
	

}
